<template>
	<view class="container ">
		<!-- <uni-nav-bar color="#333" @clickLeft="leftFun" :border="false" left-icon="left" backgroundColor="#fff"
			title="购物车"></uni-nav-bar> -->
			
		<view class="bg">
			
		</view>
		<shop-cart style="margin-top: -100rpx;" ref="shopCart1" :selectKey="selectKey" :cart-list="cartList" :select-cart-list="selectCartList"
			@operateCount="operateCount" @triggerSelect="triggerSelect" @triggerSelectAll="triggerSelectAll"
			@toDetail="toDetail" @submit="submit" @deleteCart="cartDelete"></shop-cart>
	</view>
</template>

<script>
	import {
		qzApi
	} from '@/config/request.js';
	export default {
		data() {
			return {
				selectKey: "id",
				selectCartList: [],
				cartList: [],
				shopCart1: null,
				selectShop: []

			}
		},
		onLoad() {
			this.carList()
		},
		onShow() {
			this.carList()
		},
		methods: {
			// 购物车提交
			async submit() {
				const selectList = this.cartList.filter((item) => this.selectCartList.includes(item[this.selectKey]))

				if (selectList.length <= 0) {
					uni.showToast({
						title: '您还没有选择药品',
						icon: "none",
						duration: 2000
					})
				} else {
					uni.removeStorageSync("describe")
					uni.removeStorageSync("describe1")
					let allNum = 0
					let map = {}
					selectList.forEach((subItem, subindex) => {
						allNum += subItem.quantity
						for (let index = 0; index < this.cartList.length; index++) {
							const item = this.cartList[index];
							if (item.id == subItem.id) {
								map[item.id] = item.quantity
							}
						}
					})
					const getTradeId = await qzApi.getFyOrderSn();

					if (getTradeId.data) {
						let form = {
							cartItemMap: map,
							orderSn: getTradeId.data.orderSn
						}
						const createOrderData = await qzApi.createOrder(form);
						this.carList()
						if (createOrderData.code == 0) {
							
							if (this.isAllSelectedDrugsAreOTC()) {
								uni.navigateTo({
									url: '/page1/order/index?id=' + createOrderData.data,
								})
							} else {
								uni.navigateTo({
									url: '/page1/product/describe?id=' + createOrderData.data,
								})
							}

						} else {
							uni.showToast({
								title: createOrderData.msg,
								icon: "none",
								duration: 2000
							})
						}
					}

				}

			},

			// 是不是所有选择的药品的都是OTC
			isAllSelectedDrugsAreOTC() {
				for (let index = 0; index < this.selectShop.length; index++) {
					const element = this.selectShop[index];
					console.log('isAllSelectedDrugsAreOTC index element', index, element);
					if (element.rxType != 0 && element.rxType != '无需处方') {
						return false
					}
				}
				return true
			},

			// 详情页
			toDetail() {

			},
			// 删除
			// cartDelete() {
			// 	// this.selectCartList.splice(index, 1)
			// 	console.log(this.selectCartList)
			// 	this.deleteGoodsInCart
			// 	// this.shopCart1.calculatePrice()
			// },

			async cartDelete(id) {
				try {
					let form = {
						cartItemIds: [id]
					}
					const data = await qzApi.deleteGoodsInCart(form);
					if (data.code == 0) {
						this.carList()
					}
				} catch (error) {
					// 处理错误
					console.error('获取用户信息失败:', error);
				}
			},



			// 加减
			async operateCount(params) {
				const {
					item,
					type
				} = params
				if (type === 'add') {
					item.quantity += 1;
					let  form={
						cartItemId:item.id,
						quantity:item.quantity
					}
					const data = await qzApi.addOrSubGoods(form);
					console.log(this.selectCartList)
					
					
				} else if (type === 'del' && item.quantity >= 2) {
					item.quantity -= 1;
					let  form={
						cartItemId:item.id,
						quantity:item.quantity
					}
					const data = await qzApi.addOrSubGoods(form);
					
					
					console.log(this.selectCartList)
				}
				// this.shopCart1.calculatePrice()
			},
			//单选
			triggerSelect(item) {
				const findIndex = this.selectCartList.findIndex(t => t === item[this.selectKey])
				if (findIndex === -1) {
					this.selectCartList.push(item[this.selectKey])
					this.selectShop.push(item)
				} else {
					this.selectCartList.splice(findIndex, 1)
					this.selectShop.splice(findIndex, 1)
				}

				console.log(this.selectShop)
			},
			//多选

			triggerSelectAll(selectAll) {
				if (selectAll) {
					this.selectCartList = []
					this.selectShop = []
				} else {
					this.selectCartList = this.cartList.map(item => item[this.selectKey])
					let data=JSON.stringify(this.cartList)
					this.selectShop =JSON.parse(data);

					console.log(this.selectShop)
				}
			},
			// 返回
			leftFun() {
				uni.switchTab({
					url: "/pages/index/index"
				})
			},
			//商品列表
			async carList() {
				try {
					let form = {
						displayMode: 1,
						name: '',
						pageNum: 1,
						pageSize: 6
					}
					const data = await qzApi.getGoodsInCart("");
					if(data.data.length==0){
						this.cartList=[];
						this.selectCartList=[];
					}else{
						this.cartList = data.data;
					}
					// productList.value = data.data
				} catch (error) {
					// 处理错误
					console.error('获取用户信息失败:', error);
				}
			}

		}
	}
</script>


<style lang="scss" scoped>
	page {
		background: rgb(246, 246, 246);
		padding-bottom: 150rpx;
	}
	
	.container {
	   padding-bottom: calc(constant(safe-area-inset-bottom) + 50rpx); /*兼容 IOS<11.2*/
	   padding-bottom: clac(env(safe-area-inset-bottom) + 50rpx); /*兼容 IOS>11.2*/
	   padding: 0;
	  }
	  .bg{
		  height: 200rpx;
		  background: linear-gradient( 180deg, #236EFF 0%, rgba(35,110,255,0) 100%);
	  }
	
</style>